<?php

namespace App\Models;

use App\Jobs\FleaUserWithdrawal;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

/**
 * Class FleaWithdrawal
 * @package App\Models
 * 用户提现模型
 */
class FleaWithdrawal extends Model
{
    protected $table = 'flea_withdrawal';

    protected $guarded = [];

    protected $casts = [
        'created_at' => 'datetime:Y-m-d H:i:s'
    ];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    //订单状态
    public function getStateTextAttribute()
    {
        $text = '';
        if ($this->state == 1) {
            $text = '待审核';
        } elseif ($this->state == 2) {
            $text = '已通过';
        } elseif ($this->state == 3) {
            $text = '已拒绝';
        }

        return $this->attributes['state_text'] = $text;
    }


    /**
     * @param $page
     * @param $limit
     * @return mixed
     * @throws \Exception
     * 获取用户提现列表
     */
    public static function getWithdrawalList($page, $limit)
    {
        $user_id = auth('user')->id();
        if (!$user_id) {
            throw new \Exception('请先登录');
        }

        $offset = ($page == 1) ? 0 : ($page - 1) * $limit;

        $data = FleaWithdrawal::query()->select('id', 'money', 'state', 'created_at', 'examine_at', 'mark')->offset($offset)->limit($limit)->get();

        $response['data'] = $data;
        $response['count'] = FleaWithdrawal::query()->count();
        $response['pages'] = ceil($response['count'] / $limit);

        return $response;
    }

    /**
     * @param $id int 提交审核的ID
     * @param $state  int 1通过  2拒绝
     * @param $mark string 理由
     * 后台审核用户提现
     */
    public static function examine($id, $state, $mark)
    {
        DB::beginTransaction();
        try {
            $data = self::query()->findOrFail($id);

            if ($data['state'] != 1) {
                throw new \Exception('订单已审核');
            }

            //审核时间
            $data->examine_at = date('Y-m-d H:i:s');

            if ($state == 1) {
                //通过
                $data->state = 2;
                //账户转账  插入到消息队列 1分钟后执行  测试环境设置0
                FleaUserWithdrawal::dispatch($data->id)->onQueue('FleaUserWithdrawal')->delay(now()->addMinutes(0));
                //记录用户通知
                FleaNotice::query()->create([
                    'user_id' => $data->user_id,
                    'type' => 4,
                    'classify' => 2,
                    'msg' => '您申请提现的' . $data->money . '元审核通过'
                ]);

            } elseif ($state == 2) {
                //拒绝
                $data->state = 3;
                $data->mark = $mark;
                //拒绝的钱返回用户的冻结资金里  flea_frozen_money
                User::query()->where('id', $data->user_id)->increment('flea_frozen_money', $data->money);
            }
            $data->save();

            DB::commit();
        } catch (\Exception $exception) {
            DB::rollBack();
            throw new \Exception($exception->getMessage());
        }
    }
}
